using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TidhiGanitam.DataModel;
using TidhiGanitam.Ganitam;
using System.Xml;
using System.Xml.Serialization;
using System.IO;


namespace TidhiGanitam
{
    public partial class Form1 : Form
    {
        Ravi ravi = new Ravi();
        Chandrudu chandrudu = new Chandrudu();
        Dinamu dinamu;
        Panchanzyamu panchanzyamu;
        DisplayProperties m_DisplayProperties = new DisplayProperties();
        History m_PresentHistory;
        int CurrentYear = 0;
        StringBuilder sbPanchanzyamu = new StringBuilder();
        /* Format is as follows
         * 0    - Date
         * 1    - Tidhi
         * 2    - Varamu i.e. Monday, Tuesday..
         * 3    - Tidhi times with esyamu and in time format also
         * 4    - Nakshatramu with time format also
         * 5    - Karanamu
         * 6    - Yogamu
         * 7    - Vishaghadiyalu, Tyajyamu in time format also
         * 8    - Dinamu
         * 9    - Arkaha
         * 10   - Durmuhurtaalu with time format
         * 11   - Bhukti
         * 12   - Suryodayamu in time format
         * 13   - Suryastamamu in time format */
        string PanchanzyamuFormat = "{0}: {1} {15} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12} Su.Ud:{13} Su.Ast:{14}";

        public Form1()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //Clear the UI 
            ClearUIValues();
            //Initialize panchanzyamu values
            Initialize();
            //
            CurrentYear = Convert.ToInt32(txt3_Year.Text);
            //First find the history to populate already calculated values of iyanamsa
            LoadHistory();
            YearDetails SelectedYearDetails = GetYearDetails(CurrentYear);
            if (SelectedYearDetails.Year == 0)
            {
                //user doesn't want to continue. just return
                MessageBox.Show("User cancelled the action");
                return;
            }
            TidhiGanitam.Ganitam.Abdapudu abdp = new TidhiGanitam.Ganitam.Abdapudu(SelectedYearDetails.Year,
                                                    Constants.Rajahmundry, SelectedYearDetails.UgadiDate);
            abdp.DoAbdapaSamskaram();
            if (abdp.SamskaramDone)
            {
                txt3_Varam.Text = abdp.VaramTidhi.VaramItem.ToString();
                txt3_Tidhi.Text = abdp.VaramTidhi.TidhiItem.ToString();
                txt3_chalanaphalam.Text = abdp.ChalanaPhalam.ToString() + " :: " +
                                            abdp.ChalanaPhalamSign.ToString();
                txt3_Tidhulu.Text = abdp.Tidhulu.ToString();
                /* Start calculating ravi values
                 * */
                ravi.DoRaviSamskaram(abdp.SalivahanaYear, abdp, SelectedYearDetails);

                ravi.DoRaviRoutinesForYear();

                BindRaviValuesToTab();
                /*  End of Ravi Calculations */
                /*  Start of Chandra Calculations */
                chandrudu.DoChandraSamskaram(abdp.SalivahanaYear, abdp, ravi, SelectedYearDetails);

                chandrudu.DoChandraRoutinesForYear(ravi);

                BindChandraValuesToTab();

                //Write to file\
                //WriteRaviChandraValuestoFile();
                /************************/
                /* End of Chandra calculations */
                /* Start of Dinamu Calculations */
                dinamu = new Dinamu(abdp.SalivahanaYear, SelectedYearDetails, ravi);
                dinamu.DoDailyRoutinesForYear();
                BindDinamuValuesToTab();
                /* End of Dinamu Calculations */
                /* Start of Tidhi Calculations*/
                
                panchanzyamu = new Panchanzyamu(abdp.SalivahanaYear, SelectedYearDetails, ravi.DailyCalculations,
                                    chandrudu.DailyCalculations, dinamu.DailyCalculations);
                panchanzyamu.DoDailyRoutinesForYear();

                BindPanchanzyamuValuesToTab();
                /* End of Tidhi Calculations */
            }
            //Write the output to word document
            WritePanchanzyamuToWordDoc();
            //Update History For current year and Next year
            UpdateHistory();
            //Now Save the history
            //SaveHistory();
            //Clear un wanted Values
            ClearValues();
        }

        private void Initialize()
        {
            ravi = new Ravi();
            chandrudu = new Chandrudu();
            m_DisplayProperties = new DisplayProperties();
            CurrentYear = 0;
            sbPanchanzyamu = new StringBuilder();
        }
        private void WritePanchanzyamuToWordDoc()
        {
            string DirPath = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\panchanzyamu";
            if (!Directory.Exists(DirPath))
            {
                Directory.CreateDirectory(DirPath);
            }
            object FileName = DirPath + "\\panchanzyamu-" + CurrentYear.ToString() + ".doc";
            Word.ApplicationClass wordApp = new Word.ApplicationClass();
            object ReadOnly = false;
            object isVisible = true;
            object Missing = System.Reflection.Missing.Value;
            Word.Document wordDoc = wordApp.Documents.Add(ref Missing, ref Missing, ref Missing, ref isVisible);
            wordDoc.Activate();
            wordApp.Selection.Font.Name = "Palatino Linotype";
            wordApp.Selection.Font.Size = 10;
            wordApp.Selection.Font.Bold = 0;
            foreach (PanchanzyamuRoutineItem eachDay in panchanzyamu.DailyCalculations)
            {
                StringBuilder sbPanchanzyamuValues = GetPanchanzyamuDetailsString(eachDay);
                wordApp.Selection.TypeText(sbPanchanzyamuValues.ToString());
                //wordApp.Selection.TypeParagraph();
            }
            wordDoc.SaveAs(ref FileName, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing,
                ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing,
                ref Missing, ref Missing, ref Missing);
                            

            ((Word.ApplicationClass)wordApp).Quit(ref Missing, ref Missing, ref Missing);
            
            
        }
        private StringBuilder GetPanchanzyamuDetailsString(PanchanzyamuRoutineItem eachDay)
        {
            /* Format is as follows
             * 0    - Bhagamu
            * 1    - Date
            * 2    - Varamu i.e. Monday, Tuesday..
            * 3    - Tidhi
            * 4    - Nakshatramu with time format also
            * 5    - Karanamu
            * 6    - Yogamu
            * 7    - Vishaghadiyalu, Tyajyamu in time format also
            * 8    - Amruta Ghadiyalu 
            * 8    - Dinamu
            * 9    - Arkaha
            * 10   - Durmuhurtaalu with time format
            * 11   - Bhukti
            * 12   - Suryodayamu in time format
            * 13   - Suryastamamu in time format */
            StringBuilder retString = new StringBuilder();
                DinamuRoutineItem CurrentDinamu = (DinamuRoutineItem)dinamu.DailyCalculations[eachDay.Bhagam];
                string TidhiString = string.Empty;
                string NakshatramuString = string.Empty;
                string KaranamuString = string.Empty;
                string YogamuString = string.Empty;
                string VarjyamuString = string.Empty;
                string DurmuhuraaluString = string.Empty;
                string AmrutaGhadiyaluString = string.Empty;
                string DinamuString = string.Empty;
                string BhuktiString = string.Empty;

                foreach(TidhiDetails dayTidhulu in eachDay.TidhiRoutine.TidhuluDetails)
                {
                    TidhiString += dayTidhulu.GetTidhiDetailsString(new Rasi(CurrentDinamu.Suryodayam)) + " ";
                }
                foreach(NakshatramuDetails dayNakshatramulu in eachDay.NakshatramuRoutine.NakshatramuDetails)
                {
                    NakshatramuString += dayNakshatramulu.GetNakshatramuDetailsString(new Rasi(CurrentDinamu.Suryodayam)) + " ";
                }
                KaranamuString = eachDay.KaranamuRoutine.KaranamuItem.GetKaranamuString();
                foreach(YogamuDetails dayYogamulu in eachDay.YogamuRoutine.YogamuDetails)
                {
                    YogamuString += dayYogamulu.GetYogamuDetailsString(new Rasi(CurrentDinamu.Suryodayam)) + " ";
                }
                foreach(VarjyamuDetails dayVarjyamulu in eachDay.VarjyamuRoutine.VarjyamuDetails)
                {
                    VarjyamuString += dayVarjyamulu.GetVarjyamuDetailsString(new Rasi(CurrentDinamu.Suryodayam)) + " ";
                }
                foreach(DurmuhurtamuDetails dayDurmuhurtaalu in eachDay.Durmuhurtaalu.DurmuhurtamuDetails)
                {
                    DurmuhuraaluString += dayDurmuhurtaalu.GetDurmuhurtamuDetailsString() + " ";
                }
                foreach (AmrutaGhadiyaluDetails dayAmrutaGhadiyalu in eachDay.AmrutaGhadiyalu.AmrutaGhadiyaluDetails)
                {
                    AmrutaGhadiyaluString += dayAmrutaGhadiyalu.GetAmrutaGhadiyaluString() + " ";
                }
                DinamuString = "Di." + CurrentDinamu.DinaPramanam.DisplayFormat(Enums.DisplayFormat.LV) + " ";
                BhuktiString = eachDay.Bhukti.GetBhuktiDetailsString();

                retString.AppendFormat(PanchanzyamuFormat ,
                    eachDay.Bhagam.ToString(),                      //0     - Bhagamu
                    eachDay.Date.ToString("dd-MMM-yy"),             //1     - Date
                    eachDay.Date.DayOfWeek.ToString(),              //2     - Varamu - Monday, Tuesday...
                    TidhiString,                                    //3     - Tidhulu string
                    NakshatramuString,                              //4     - Nakshatramu string
                    KaranamuString,                                 //5     - Karanamu string
                    YogamuString,                                   //6     - Yogamu string
                    VarjyamuString,                                 //7     - Varjyamu string
                    AmrutaGhadiyaluString,                          //8     - Amruta Ghadiyalu
                    DinamuString,                                   //9 - Dina pramanamu
                    eachDay.Arkaha.ArkahaDetails.GetArkahaDetailsString(), //10     - Arkaha string
                    DurmuhuraaluString,                                             //11    - Durmuhurtaalu string
                    BhuktiString,                                                   //12    - Bhukti
                    CurrentDinamu.Suryodayam.DisplayFormat(Enums.DisplayFormat.LV), //13    - Suryodayamu
                    CurrentDinamu.Suryastamam.DisplayFormat(Enums.DisplayFormat.LV), //14    - Suryastamamu
                    Environment.NewLine                                             //15    - Create a new line
                    );
                retString.AppendLine("");
                retString.AppendLine("");

            return retString;
        }
        private void ClearUIValues()
        {
            DGAmruGhadiyaluValues.Rows.Clear();
            DGArkahaValues.Rows.Clear();
            DGChandraValues.Rows.Clear();
            DGDinamuValues.Rows.Clear();
            DGDurmuhurtaaluValues.Rows.Clear();
            DGNakshatramuValues.Rows.Clear();
            DGRaviValues.Rows.Clear();
            DGTidhuluValues.Rows.Clear();
            DGVarjyamuValues.Rows.Clear();
            DGYogamuValues.Rows.Clear();
        }

        private void ClearValues()
        {
            //ravi.DailyCalculations.Clear();
            //chandrudu.DailyCalculations.Clear();
            //dinamu.DailyCalculations.Clear();
            //panchanzyamu.DailyCalculations.Clear();
            //m_PresentHistory.YearsDetails.Clear();
            ravi = null;
            chandrudu = null;
            dinamu = null;
            panchanzyamu = null;
            m_PresentHistory = null;
            CurrentYear = 0;
        }

        private void UpdateHistory()
        {
            //Delete next year details if present
            int HistoryIndex = 0;
            int UpperLimit = m_PresentHistory.YearsDetails.Count;
            for (; HistoryIndex < UpperLimit; HistoryIndex++)
            {
                if (((YearDetails)m_PresentHistory.YearsDetails[HistoryIndex]).Year == CurrentYear + 1)
                {
                    m_PresentHistory.YearsDetails.RemoveAt(HistoryIndex);
                    break;
                }
            }
            HistoryIndex = 0;
            UpperLimit = m_PresentHistory.YearsDetails.Count;
            double PreviousRaviRasi = -1;
            YearDetails NextYearDetails = new YearDetails();
            NextYearDetails.AdhikaMasamu = -1;
            for (; HistoryIndex < UpperLimit; HistoryIndex++)
            {
                YearDetails CurrentYearDetails = (YearDetails) m_PresentHistory.YearsDetails[HistoryIndex];
                if (CurrentYearDetails.Year == CurrentYear)
                {
                    for (int index = 25; index < panchanzyamu.DailyCalculations.Count; index++)
                    {
                        RaviRoutineItem RaviItem = (RaviRoutineItem)ravi.DailyCalculations[index];
                        TidhiRoutineItem TidhiItem = (TidhiRoutineItem)((PanchanzyamuRoutineItem)panchanzyamu.DailyCalculations[index]).TidhiRoutine;
                        if (TidhiItem.CurrentMasamu == 0 && TidhiItem.CurrentTidhi == 0)
                        {
                            CurrentYearDetails.DaysInYear = TidhiItem.Bhagam;
                            NextYearDetails.Year = CurrentYearDetails.Year + 1;
                            NextYearDetails.UgadiDate = ((DinamuRoutineItem)dinamu.DailyCalculations[TidhiItem.Bhagam]).Date;
                            NextYearDetails.UgadiIyanamsamu = new Rasi(((DinamuRoutineItem)dinamu.DailyCalculations[TidhiItem.Bhagam]).Iyanamsamu);
                            m_PresentHistory.YearsDetails.Insert(HistoryIndex + 1, NextYearDetails);
                            break;
                        }

                        //if (TidhiItem.CurrentTidhi == 17 || TidhiItem.CurrentTidhi == 18)
                        //{
                        //    PreviousRaviRasi = RaviItem.MadyamaRavi.Rasulu;
                        //}

                        //if (TidhiItem.CurrentTidhi == 19)
                        //{
                        //    if (PreviousRaviRasi != RaviItem.MadyamaRavi.Rasulu)
                        //    {
                        //        NextYearDetails.AdhikaMasamu = TidhiItem.CurrentMasamu;
                        //    }
                        //}
                    }
                }
            }
        }

        private YearDetails GetYearDetails(int Year)
        {
            YearDetails returnValue = new YearDetails();
            foreach (YearDetails yearDetails in m_PresentHistory.YearsDetails)
            {
                if (yearDetails.Year == Year)
                {
                    returnValue = new YearDetails(yearDetails);
                }
            }
            if (returnValue.Year == 0)
            {
                //We could not find the year details so Ask the user to input
                GetIyanamsaDetails IyanamsaDetails = new GetIyanamsaDetails();
                IyanamsaDetails.ShowDialog(this);
                if (IyanamsaDetails.DialogResult == DialogResult.OK)
                {
                    returnValue = new YearDetails(IyanamsaDetails.UgadiDetails);
                    //Add this to history to help the updates later
                    m_PresentHistory.YearsDetails.Add(returnValue);
                }
            }
            return returnValue;
        }

        private void LoadHistory()
        {
            string DataDirectory = "History.txt";
            if (File.Exists(DataDirectory))
            {
                using (TextReader HistoryReader = new StreamReader(DataDirectory, Encoding.Default))
                {
                    XmlSerializer Serializer = new XmlSerializer(typeof(History));
                    m_PresentHistory = (History)Serializer.Deserialize(HistoryReader);
                }
            }
            else
            { 
                //Till now there is no history for the calculations.
                m_PresentHistory = new History();
            }
        }

        private void SaveHistory()
        {
            string DataDirectory = "History.txt";
            XmlSerializer Serializer = new XmlSerializer(typeof(History));
            using (TextWriter HistoryWriter = new StreamWriter(DataDirectory, false))
            {
                Serializer.Serialize(HistoryWriter, m_PresentHistory);
            }
        }

        private void BindPanchanzyamuValuesToTab()
        {
            if (panchanzyamu != null)
            {
                foreach (TidhiGanitam.DataModel.PanchanzyamuRoutineItem PanchanzyamuRoutine in panchanzyamu.DailyCalculations)
                {
                    foreach (TidhiGanitam.DataModel.TidhiDetails TidhiDetail in PanchanzyamuRoutine.TidhiRoutine.TidhuluDetails)
                    {
                            DGTidhuluValues.Rows.Add(new string[]{
                                PanchanzyamuRoutine.Bhagam.ToString(),
                                PanchanzyamuRoutine.Date.ToString("dd-MMM-yyyy"),
                                TidhiDetail.Masamu,
                                TidhiDetail.Tidhi,
                                TidhiDetail.EndTime.DisplayFormat(Enums.DisplayFormat.LV),
                                TidhiDetail.TidhiState.ToString(),
                                PanchanzyamuRoutine.KaranamuRoutine.KaranamuItem.Karanamu + " " + PanchanzyamuRoutine.KaranamuRoutine.KaranamuItem.KaranamuTime.DisplayFormat(Enums.DisplayFormat.LV)
                            });

                    }
                    foreach (TidhiGanitam.DataModel.NakshatramuDetails nakshatramuDetail in PanchanzyamuRoutine.NakshatramuRoutine.NakshatramuDetails)
                    {
                        DGNakshatramuValues.Rows.Add(new string[]{
                                PanchanzyamuRoutine.Bhagam.ToString(),
                                PanchanzyamuRoutine.Date.ToString("dd-MMM-yyyy"),
                                nakshatramuDetail.Nakshatramu,
                                nakshatramuDetail.EndTime.DisplayFormat(Enums.DisplayFormat.LV),
                                nakshatramuDetail.NakshatramuState.ToString(),
                                nakshatramuDetail.TotalSpan.DisplayFormat(Enums.DisplayFormat.LV)
                            });

                    }

                    foreach (TidhiGanitam.DataModel.YogamuDetails YogamuDetail in PanchanzyamuRoutine.YogamuRoutine.YogamuDetails)
                    {
                        DGYogamuValues.Rows.Add(new string[]{
                                PanchanzyamuRoutine.Bhagam.ToString(),
                                PanchanzyamuRoutine.Date.ToString("dd-MMM-yyyy"),
                                YogamuDetail.Yogamu,
                                YogamuDetail.EndTime.DisplayFormat(Enums.DisplayFormat.LV),
                                YogamuDetail.YogamuState.ToString()
                            });
 
                    }
                     foreach (TidhiGanitam.DataModel.VarjyamuDetails Varjyamu in PanchanzyamuRoutine.VarjyamuRoutine.VarjyamuDetails)
                     {
                         DGVarjyamuValues.Rows.Add(new string[] {
                            PanchanzyamuRoutine.Bhagam.ToString(),
                            PanchanzyamuRoutine.Date.ToString("dd-MMM-yyyy"),
                            Varjyamu.Varjyamu,
                            Varjyamu.Tyajyamu                            
                            });
                     }
                     foreach (TidhiGanitam.DataModel.AmrutaGhadiyaluDetails AmruGhadiyalu in PanchanzyamuRoutine.AmrutaGhadiyalu.AmrutaGhadiyaluDetails)
                     {
                         DGAmruGhadiyaluValues.Rows.Add(new string[] {
                            PanchanzyamuRoutine.Bhagam.ToString(),
                            PanchanzyamuRoutine.Date.ToString("dd-MMM-yyyy"),
                            AmruGhadiyalu.AmrutaGhadiyalu                  
                            });
                     }
                     foreach (TidhiGanitam.DataModel.DurmuhurtamuDetails Durmuhurtaalu in PanchanzyamuRoutine.Durmuhurtaalu.DurmuhurtamuDetails)
                     {
                         DGDurmuhurtaaluValues.Rows.Add(new string[] {
                             PanchanzyamuRoutine.Bhagam.ToString(),
                             PanchanzyamuRoutine.Date.ToString("dd-MMM-yyyy"),
                             PanchanzyamuRoutine.Date.DayOfWeek.ToString(),
                             Durmuhurtaalu.DurmuhurtaSamayamu.ToString(),
                             Durmuhurtaalu.StartTimeInHours.DisplayFormat(Enums.DisplayFormat.BLV),
                             Durmuhurtaalu.EndTimeInHours.DisplayFormat(Enums.DisplayFormat.BLV)
                         });
                     }
                    //Because we have only one Arkaha, we need not run a loop here
                     if (PanchanzyamuRoutine.Arkaha.ArkahaDetails.ArkahaTime > new Rasi())
                     {
                         DGArkahaValues.Rows.Add(new string[] {
                         PanchanzyamuRoutine.Bhagam.ToString(),
                         PanchanzyamuRoutine.Date.ToString("dd-MMM-yyyy"),
                         PanchanzyamuRoutine.Arkaha.ArkahaDetails.Nakshatramu + " " + PanchanzyamuRoutine.Arkaha.ArkahaDetails.Padamu,
                         PanchanzyamuRoutine.Arkaha.ArkahaDetails.ArkahaTime.DisplayFormat(Enums.DisplayFormat.BLV) + 
                         "       [" + ((RaviRoutineItem)ravi.DailyCalculations[PanchanzyamuRoutine.Bhagam]).MadyamaRavi.ToString() + "] " +
                         ((RaviRoutineItem)ravi.DailyCalculations[PanchanzyamuRoutine.Bhagam]).Gati.DisplayFormat(Enums.DisplayFormat.LVP)
                     });

                     }
                    //because we have only one bhukti we need not run a loop here
                    DGBhuktiDetails.Rows.Add(new string[] {
                        PanchanzyamuRoutine.Bhagam.ToString(),
                        PanchanzyamuRoutine.Date.ToString("dd-MMM-yyyy"),
                        PanchanzyamuRoutine.Bhukti.GetBhuktiDetailsString()
                    });

                 }
            }
        }

        private void BindDinamuValuesToTab()
        {
            if(dinamu !=null)
                foreach (TidhiGanitam.DataModel.DinamuRoutineItem DinamuRoutine in dinamu.DailyCalculations)
                {
                    DGDinamuValues.Rows.Add(new string[]{
                        DinamuRoutine.Bhagam.ToString(),
                        DinamuRoutine.Date.ToString("dd-MMM-yyyy"),
                        DinamuRoutine.RojuPramanam.DisplayFormat(Enums.DisplayFormat.LV),
                        DinamuRoutine.DinaPramanam.ToString(),
                        DinamuRoutine.Dinardhamu.ToString(),
                        DinamuRoutine.Suryodayam.ToString(),
                        DinamuRoutine.Suryastamam.ToString()
                    });
                }
        }

        private void BindChandraValuesToTab()
        {
            ch_Sayn_Mdm_Chndr.Text = chandrudu.MadyamaChandrudu.ToString();
            Ch_Mdm_Chndr_Kndrm.Text = chandrudu.ChandraKendraGati.ToString();
            Ch_Sayn_Rhvu.Text = chandrudu.SayanaCShudhaRahuvu.ToString();
            StringBuilder sbChandra = new StringBuilder();
            foreach (TidhiGanitam.DataModel.ChandraRoutineItem ChndrRoutine in chandrudu.DailyCalculations)
            {
                DGChandraValues.Rows.Add(new string[]{
                    ChndrRoutine.Bhagam.ToString(),
                    ChndrRoutine.Date.ToString("dd-MMM-yyyy"),
                    ChndrRoutine.ChandraKendram.ToString(),
                    ChndrRoutine.SpastaChandrudu.ToString(),
                    ChndrRoutine.ChandraDinaGatiPhalam.DisplayFormat(Enums.DisplayFormat.LV)
                });
            }

        }
        public void WriteRaviChandraValuestoFile()
        {
            StringBuilder sbRavi = new StringBuilder();
            for (int i = 0; i < 370; i++)
            {
                RaviRoutineItem RviRoutine = (RaviRoutineItem)ravi.DailyCalculations[i];
                ChandraRoutineItem ChndrRoutine = (ChandraRoutineItem)chandrudu.DailyCalculations[i];
                sbRavi.AppendFormat("{2}-{3}:: {0}\t{1} --- {4}\t{5}{6}", RviRoutine.MadyamaRavi.ToString(), RviRoutine.RaviKendram.ToString(),
                    RviRoutine.Bhagam.ToString(), RviRoutine.Date.ToString("dd-MMM-yy"),
                    ChndrRoutine.ChandraKendram.ToString(), ChndrRoutine.SpastaChandrudu.ToString(),Environment.NewLine);
            }
            string DataDirectory = "Ravi.txt";
            using (TextWriter HistoryWriter = new StreamWriter(DataDirectory, false))
            {
                HistoryWriter.WriteLine(sbRavi.ToString());
            }
        }
        private void BindRaviValuesToTab()
        {
            Rv_Nir_Md_Rv.Text = ravi.MadyamaRavi.ToString();
            Rv_Nir_Rv_Mdcha.Text = ravi.RaviMandochha.ToString();
            Rv_Rav_kndrm.Text = ravi.RaviKendram.ToString();
            StringBuilder sbRavi = new StringBuilder();
            foreach (TidhiGanitam.DataModel.RaviRoutineItem RviRoutine in ravi.DailyCalculations)
            {
                DGRaviValues.Rows.Add(new string[] { 
                    RviRoutine.Bhagam.ToString(),
                    RviRoutine.Date.ToString("dd-MMM-yyyy"),
                    RviRoutine.RaviKendram.ToString(),
                    RviRoutine.MadyamaRavi.ToString(),
                    RviRoutine.Gati.DisplayFormat(Enums.DisplayFormat.LVP),
                    RviRoutine.ChandraPhalam.DisplayFormat(Enums.DisplayFormat.RBLVP),
                    RviRoutine.Karnam.RawValue.ToString()
                });
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Rasi a = new Rasi(0, 4, 5, 10);
            Rasi b = new Rasi(11, 3, 4, 5);
            txt3_Year.Text = ((Rasi)(a - b)).ToString();
        }
    }
}